package org.infodavid.common.model.impl;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.infodavid.common.model.IImageData;
import org.infodavid.common.model.ITemplate;
import org.infodavid.common.model.ITemplateCategory;
import org.infodavid.common.model.ITemplateVersion;
import org.infodavid.common.model.IUser;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.model.components.IOwnerDetails;

/**
 * The Class TemplateVersionImpl.
 */
public class TemplateVersionImpl extends AbstractVersionedObject<Long> implements ITemplateVersion {

	/** serialVersionUID. */
	private static final long serialVersionUID = 6523279718688638432L;

	/** The category. */
	private ITemplateCategory category;

	/** The comment. */
	private String comment;

	/** The subject. */
	private String subject;

	/** The data. */
	private byte[] data;

	/** The template. */
	private ITemplate template;

	/** The encoding. */
	private String encoding;

	/** The owner details. */
	private IUser user;

	/** The images. */
	private Map<String,IImageData> images;

	/**
	 * constructor.
	 * @throws Exception the exception
	 */
	protected TemplateVersionImpl() throws Exception {
		super();
	}

	/**
	 * constructor.
	 * @param keyArg the key
	 * @throws Exception the exception
	 */
	protected TemplateVersionImpl(final Long keyArg) throws Exception {
		this();

		setKey(keyArg);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ICategorizable#getCategory()
	 */
	@Override
	public ITemplateCategory getCategory() {
		return category;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.ICommented#getComment()
	 */
	@Override
	public String getComment() {
		return comment;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ITemplateVersion#getImages()
	 */
	@Override
	public Map<String,IImageData> getImages() {
		return images;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ITemplateVersion#getData()
	 */
	@Override
	public byte[] getData() {
		return data;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.ITemplateVersion#getSubject()
	 */
	@Override
	public String getSubject() {
		return subject;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ITemplateVersion#getTemplate()
	 */
	@Override
	public ITemplate getTemplate() {
		return template;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.ITemplateVersion#getEncoding()
	 */
	@Override
	public String getEncoding() {
		return encoding;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.ITemplateVersion#getUser()
	 */
	@Override
	public IUser getUser() {
		return user;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		comment = null;
		subject = null;
		data = null;
		category = null;
		encoding = null;
		user = null;
		images = new HashMap<String,IImageData>();
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.ICategorizable#setCategory(org.infodavid.common.model.ICategory)
	 */
	@Override
	public void setCategory(final ITemplateCategory value) {
		if (!equals(category, value)) {
			final Object old = category;
			category = value;
			markModified(CATEGORY, old, category);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.ITemplateVersion#setSubject(java.lang.String)
	 */
	@Override
	public void setSubject(final String value) {
		if (!equals(subject, value)) {
			final Object old = subject;
			subject = value;
			markModified(SUBJECT, old, subject);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ICommented#setComment(java.lang.String)
	 */
	@Override
	public void setComment(final String value) {
		if (!equals(comment, value)) {
			final Object old = comment;
			comment = value;
			markModified(COMMENT, old, comment);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.ITemplateVersion#setData(byte[])
	 */
	@Override
	public void setData(final byte[] values) {
		if (!equals(data, values)) {
			final Object old = data;
			data = values;
			markModified(DATA, old, data);
		}
	}

	/**
	 * Sets the images.
	 * @param values the values
	 */
	protected void setImages(final Map<String,IImageData> values) {
		images = values;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.ITemplateVersion#setTemplate(org.infodavid.common.model.ITemplate)
	 */
	@Override
	public void setTemplate(final ITemplate value) {
		if (!equals(template, value)) {
			final Object old = template;
			template = value;
			markModified(ITemplate.TEMPLATE, old, template);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.ITemplateVersion#setEncoding(java.lang.String)
	 */
	@Override
	public void setEncoding(final String value) {
		if (!equals(encoding, value)) {
			final Object old = encoding;
			encoding = value;
			markModified(ENCODING, old, encoding);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.s.ITemplateVersion#setUser(org.infodavid.common.model .s.IUser)
	 */
	@Override
	public void setUser(final IUser value) {
		if (!equals(user, value)) {
			final Object old = user;
			user = value;
			markModified(IUser.USER, old, user);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[subject=");
		builder.append(subject);
		builder.append(", comment=");
		builder.append(comment);
		builder.append(", category=");

		if (category == null) {
			builder.append("null");
		}
		else {
			builder.append(category.getKey());
		}

		builder.append(", user=");

		if (user == null) {
			builder.append("null");
		}
		else {
			builder.append(user.getKey());
		}

		builder.append(", template=");

		if (template == null) {
			builder.append("null");
		}
		else {
			builder.append(template.getKey());
			builder.append(" (");
			builder.append(template.getName());
			builder.append(',');
			builder.append(template.getLanguage());
			builder.append(')');
		}

		builder.append(", encoding=");
		builder.append(encoding);
		builder.append(", images=");
		builder.append(images);
		builder.append(", data=");

		if (data == null) {
			builder.append("null");
		}
		else {
			builder.append(data.length);
			builder.append(" bytes");
		}

		builder.append(']');

		return builder;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getTemplate() == null ? 0 : getTemplate().hashCode());

		return prime * result + getRevision();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof ITemplateVersion)) {
			return false;
		}

		final ITemplateVersion other = (ITemplateVersion)obj;

		if (getTemplate() == null) {
			if (other.getTemplate() != null) {
				return false;
			}
		}
		else if (!getTemplate().equals(other.getTemplate())) {
			return false;
		}

		if (getRevision() != other.getRevision()) {
			return false;
		}

		return true;
	}

	/**
	 * Check template.
	 */
	private void checkTemplate() {
		synchronized (this) {
			if (template == null) {
				try {
					template = ModelFactory.getInstance().create(ITemplate.class);
				}
				catch (final InstantiationException e) {
					throw new Error(e);
				}
			}
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IDocumentVersion#getMimeType()
	 */
	@Override
	public String getMimeType() {
		return getTemplate() == null ? null : getTemplate().getMimeType();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#getLanguage()
	 */
	@Override
	public Locale getLanguage() {
		return getTemplate() == null ? null : getTemplate().getLanguage();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#getOwnerDetails()
	 */
	@Override
	public IOwnerDetails getOwnerDetails() {
		return getTemplate() == null ? null : getTemplate().getOwnerDetails();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.INamed#getName()
	 */
	@Override
	public String getName() {
		return getTemplate() == null ? null : getTemplate().getName();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#setMimeType(java.lang.String)
	 */
	@Override
	public void setMimeType(final String value) {
		checkTemplate();

		getTemplate().setMimeType(value);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IContentInfo#setLanguage(java.util.Locale)
	 */
	@Override
	public void setLanguage(final Locale value) {
		checkTemplate();

		getTemplate().setLanguage(value);
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.INamed#setName(java.lang.String)
	 */
	@Override
	public void setName(final String arg0) {
		checkTemplate();

		getTemplate().setName(arg0);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.ITemplateVersion#addImage(org.infodavid.common.model.IImageData)
	 */
	@Override
	public void addImage(final IImageData value) {
		getImages().put(value.getName(), value);
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.ITemplateVersion#removeImage(org.infodavid.common.model.IImageData)
	 */
	@Override
	public void removeImage(final IImageData value) {
		getImages().remove(value.getName());
	}
}
